package com.example.esquel_carpool.utils;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.DrawableRes;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.target.Target;
import com.example.esquel_carpool.R;
import com.example.esquel_carpool.glideModule.GlideApp;
import com.example.esquel_carpool.glideModule.GlideRequests;

public class ImageLoaderUtil {
    private static ImageLoaderUtil mInstance = null;

    private ImageLoaderUtil() {

    }

    public static ImageLoaderUtil getInstance() {
        if (mInstance == null) {
            synchronized (ImageLoaderUtil.class) {
                if (mInstance == null) {
                    mInstance = new ImageLoaderUtil();
                }
            }
        }
        return mInstance;
    }

    public void displayImage(ImageView imageView, String path) {
        GlideApp.with(imageView)
                .load(path)
                .centerCrop()
                .into(imageView);
    }

    public void displayImage(ImageView imageView, String path, RequestOptions arg0) {
        GlideApp.with(imageView)
                .load(path)
                .apply(arg0)
                .into(imageView);
    }

//    public void displayImageBitmap(ImageView imageView, String path, RequestOptions arg0) {
//        GlideApp.with(imageView)
//                .asBitmap()
//                .load(path)
//                .apply(arg0)
//                .into(new MsgImageTarget(imageView));
//    }


    public GlideRequests getGlideRequests(Context context) {
        return GlideApp.with(context);
    }
    public GlideRequests getGlideRequests(Fragment fragment) {
        return GlideApp.with(fragment);
    }

    public GlideRequests getGlideRequests(View view) {
        return GlideApp.with(view);
    }

    public Glide getGlideApp(Context context) {
        return GlideApp.get(context);
    }

    public void displayImage(ImageView imageView, String path, @DrawableRes int drawableId) {
        GlideApp.with(imageView)
                .load(path)
                .placeholder(drawableId)
                .error(drawableId)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .centerCrop()
                .into(imageView);
    }

    public void displayImage00(ImageView imageView, String path, @DrawableRes int drawableId) {
        GlideApp.with(imageView)
                .load(path)
                .placeholder(drawableId)
                .error(drawableId)
                .fitCenter()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(imageView);
    }

    public void displayImage22(final ImageView imageView, String path, @DrawableRes int drawableId) {
        GlideApp.with(imageView)
                .load(path)
                .placeholder(drawableId)
                .error(drawableId)
//                .fitCenter()
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
                .listener(new RequestListener<Drawable>() {
                    @Override
                    public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                        return false;
                    }

                    @Override
                    public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                        if (imageView == null) {
                            return false;
                        }
                        if (imageView.getScaleType() != ImageView.ScaleType.FIT_XY) {
                            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                        }
                        ViewGroup.LayoutParams params = imageView.getLayoutParams();
                        int vw = imageView.getWidth() - imageView.getPaddingLeft() - imageView.getPaddingRight();
                        float scale = (float) vw / (float) resource.getIntrinsicWidth();
                        int vh = Math.round(resource.getIntrinsicHeight() * scale);
                        params.height = vh + imageView.getPaddingTop() + imageView.getPaddingBottom();
                        imageView.setLayoutParams(params);
                        return false;
                    }
                })
                .into(imageView);
    }


    public void displayImageWithSize(ImageView imageView, String path, @DrawableRes int drawableId, int w, int h) {
        GlideApp.with(imageView)
                .load(path)
                .placeholder(drawableId)
                .error(drawableId)
                .override(w, h)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(imageView);
    }

    public void displayImage(Context context, String path, SimpleTarget<Drawable> mls) {
        GlideApp.with(context)
                .load(path)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(mls);
    }

    public void displayImage(ImageView imageView, String path, SimpleTarget<Drawable> mls) {
        GlideApp.with(imageView)
                .load(path)
                .placeholder(R.drawable.ic_default_image)
                .into(mls);
    }

    public void displayCircleImage(ImageView imageView, String path, @DrawableRes int drawableId) {
        GlideApp.with(imageView)
                .load(path)
                .placeholder(drawableId)
                .error(drawableId)
                .circleCrop()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(imageView);
    }

    public void displayCircleImage(ImageView imageView, String path, Drawable drawableId) {
        GlideApp.with(imageView)
                .load(path)
                .placeholder(drawableId)
                .error(drawableId)
                .circleCrop()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(imageView);
    }

    public void displayImage(ImageView imageView, String path, Drawable drawableId) {
        GlideApp.with(imageView)
                .load(path)
                .placeholder(drawableId)
                .error(drawableId)
                .centerCrop()
                .into(imageView);
    }

//    public void displayRoundedCornersImage(ImageView imageView, String path, @DrawableRes int drawableId, int radius, int marge) {
//        GlideApp.with(imageView)
//                .load(path)
//                .placeholder(drawableId)
//                .error(drawableId)
//                .transform(new RoundedCornersTransformation(radius, marge))
//                .into(imageView);
//    }
//
//    public void displayRoundedCornersImage(ImageView imageView, String path, @DrawableRes int drawableId, int radius) {
//        GlideApp.with(imageView)
//                .load(path)
//                .placeholder(drawableId)
//                .error(drawableId)
//                .transform(new RoundedCornersTransformation(radius, 0))
//                .into(imageView);
//    }
//
//    public void displayRoundedCornersImage(ImageView imageView, String path, Drawable drawableId, int radius) {
//        GlideApp.with(imageView)
//                .load(path)
//                .placeholder(drawableId)
//                .error(drawableId)
//                .transform(new RoundedCornersTransformation(radius, 0))
//                .into(imageView);
//    }
}
